package com.situ.mall.manager.config;

import com.situ.mall.manager.intercepter.AdminIntercepter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置SpringMVC
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${upload.path}")
    private  String  path;
    @Autowired
    private AdminIntercepter adminIntercepter;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //添加上传图片的映射路径
        //当客户端访问/upload/路径下的任何资源时，将从本地资源
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:"+path);
    }
    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册管理员的拦截器
        registry.addInterceptor(adminIntercepter)
                .addPathPatterns("/**")        //添加要拦截的路径
                .excludePathPatterns(
                        "/layui/**",
                        "/admin/login.html",
                        "/vercode",
                        "/api/admin/login");   //排除不需要拦截的路径
    }
}
